package com.group3.hospital.controller;/*
 *@author 时崎
 * 2022/01/04
 */

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group3.hospital.bean.User;
import com.group3.hospital.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class UserManageController {
    @Resource
    public UserService userService;
    //展示用户管理页面
    @RequestMapping("/userManageUi")
    public String userManageUi(Model model,@RequestParam(required = false,defaultValue = "1",value = "pageNum") Integer pageNum,
                               @RequestParam(defaultValue = "5",value = "pageSize") Integer pageSize){
        if(pageNum == null || pageNum <= 0){
            pageNum = 1;
        }
        if(pageSize == null){
            pageSize = 6;
        }
        PageHelper.startPage(pageNum,pageSize);
        try {
            List<User> users = userService.queryAll();
            PageInfo<User> pageInfo = new PageInfo<>(users,pageSize);
            model.addAttribute("pageInfo",pageInfo);
            model.addAttribute("users",users);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "user_manage";
    }
    //删除用户
    @RequestMapping("/deleteUser")
    public String deleteUser(@RequestParam("userId") Integer userId){
        try {
            userService.deleteById(userId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:userManageUi";
    }
}
